package de.bazinga.rotw.objects.elevator;

import com.jme.math.Vector3f;
import com.jme.scene.Controller;

public class ElevatorController extends Controller {
	private static final long serialVersionUID = 1L;
	
	private Elevator elevator = null;
	
	private float speed = 10;
	private int factor = 1;
	
	private float start = 0;
	private float end = 0;
	
	public ElevatorController(Elevator e, float speed, float start, float end) {
		this.elevator = e;
		this.speed = speed;
		
		this.start = start;
		this.end = start+end;
	}

	
	public void update(float tpf) {
		Vector3f addVec = new Vector3f(0,0,0);
		if(elevator.getDirection() == Vector3f.UNIT_X) addVec.setX(factor*speed*tpf);
		if(elevator.getDirection() == Vector3f.UNIT_Y) addVec.setY(factor*speed*tpf);
		if(elevator.getDirection() == Vector3f.UNIT_Z) addVec.setZ(factor*speed*tpf);
		

        elevator.getLocalTranslation().addLocal(addVec);
		
		float currValue = 0;
		if(elevator.getDirection() == Vector3f.UNIT_X) currValue = elevator.getLocalTranslation().getX();
		if(elevator.getDirection() == Vector3f.UNIT_Y) currValue = elevator.getLocalTranslation().getY();
		if(elevator.getDirection() == Vector3f.UNIT_Z) currValue = elevator.getLocalTranslation().getZ();
		
		if(end > start) {
			if(currValue > end) factor = -1;
			if(currValue <= start) {
				factor = 1;
				if(elevator.getActivateOnCollision()) this.setActive(false);
			}
		}else if(end < start) {
			if(currValue < end) {
				factor = -1;
				if(elevator.getActivateOnCollision()) this.setActive(false);
			}
			if(currValue >= start) factor = 1;
		}
		
	}


	public int getFactor() {
		return factor;
	}


	public void setFactor(int factor) {
		this.factor = factor;
	}

}
